// pages/infoadd.js//
import util from '../../../utils/util.js'
var app=getApp()
var host=app.globalData.host
var id=0;
function checky(y)
{
    if((y%4==0&&y%100!=0)||y%400==0)
    {
        return true;
    }
}
Page({
    /**
     * 页面的初始数据
     */
    data: {
		xmindex:0,
		xms:["请选择"],
		xm:"请选择",
        index:0,
        offices:["神经内科"],
        name:'',
        num:'',
        sex:'',
        age:0,
        phoneNum:'',
        keshi:'请选择',
        regDate:new Date(),
        treatCnt:0,
        intervalTime:0,
        status:true,
        illness:'',
        datamissing: new Array(10),
        debugg:0,
        mzh:'',
        fre:30,
        nextdate:'',
        zd:'',
		date:util.formatDate(new Date()),
		idx:0,
		ybh:'',
		bh:''
    },
    confirmName: function(e)
    {
        this.setData({
            name : e.detail.value
        })
    },
    confirmSex1: function()
    {
        this.setData({
            sex:'男'
        })
    },
    confirmSex2: function()
    {
        this.setData({
                sex:'女'
        })
    },
    confirmNum: function(e)
    {
        this.setData({
            num : e.detail.value
        })
    },
    confirmAge: function(e)
    {
        this.setData({
            age :e.detail.value
        })
    },
    confirmPhoneNum: function(e)
    {
        this.setData({
            phoneNum : e.detail.value
        })
    },
    confirmKeshi: function(e)
    {
        this.setData({
            keshi : e.detail.value
        })
    },
    bindkeshiChange: function(e)
    {
        console.log(e);
        this.data.index=e.detail.value
        this.setData({
            keshi: this.data.offices[this.data.index]
        })
	},
	bindxmChange: function(e)
    {
        console.log(e);
        this.setData({xmindex: e.detail.value})
        this.setData({
            xm: this.data.xms[this.data.xmindex]
        })
	},
    confirmdate: function(e)
    {
        this.setData({
            date : (e.detail.value)
		})
		if(e.detail.value.length==10)
		{
			var datee=this.data.date;
			var y = parseInt(datee.substring(0,4));
			var m = parseInt(datee.substring(5,7));
			var d = parseInt(datee.substring(8,10));
			var freq=parseInt(this.data.fre);
			if(m>12||m==0||d>31||d==0)
			{
				wx.showToast({
				  title: '日期错误',
				  icon:'error'
				})
			}
			else
			{
				var dates=[0,31,28,31,30,31,30,31,31,30,31,30,31];
        for(var i=0;i<freq;i++)
        {
            if(d==dates[m])
            {
				if(m!=12)
				{
                	m++;
                	d=1;
				}
				else
				{
					m=1;
					d=1;
					y++;
				}
            }
            else
            {
                d++;
            }
        }
				y=y+"";
				m=m+"";
				d=d+"";
				this.setData({
					nextdate:y+'-'+m+'-'+d
				})
			}
		}
    },
    checkdate:function(e)
    {
        var datee=this.data.date;
        var y = parseInt(datee.substring(0,4));
        var m = parseInt(datee.substring(5,7));
        var d = parseInt(datee.substring(8,10));
        var freq=parseInt(this.data.fre);
        var dates=[0,31,28,31,30,31,30,31,31,30,31,30,31];
        for(var i=0;i<freq;i++)
        {
            if(d==dates[m])
            {
				if(m!=12)
				{
                	m++;
                	d=1;
				}
				else
				{
					m=1;
					d=1;
					y++;
				}
            }
            else
            {
                d++;
            }
        }
        y=y+"";
        m=m+"";
        d=d+"";
        this.setData({
            nextdate:y+'-'+m+'-'+d
        })
	},
	confirmYbh: function(e)
    {
        this.setData({
            ybh : (e.detail.value)
        })
	},
    confirmMzh: function(e)
    {
        this.setData({
            mzh : (e.detail.value)
        })
	},
	confirmBh: function(e)
    {
        this.setData({
            bh : (e.detail.value)
        })
	},
	confirmZyh: function(e)
    {
        this.setData({
            zyh : (e.detail.value)
        })
    },
    confirmZd: function(e)
    {
        this.setData({
            zd : (e.detail.value)
        })
    },
    confirmfre: function(e)
    {
        this.setData({
            fre : parseInt(e.detail.value)
        })
        if(parseFloat(e.detail.value)!=parseInt(e.detail.value))
        {
            this.setData({
                isfreint:0
            })
        }
        else
        {
            this.setData({
                isfreint:1
            })
        }
        if(this.data.fre>=365)
        {
            this.setData({
                fretoolong:1
            })
        }
        else
        {
            this.setData({
                fretoolong:0
            })
        }
        var datee=this.data.date;
        var y = parseInt(datee.substring(0,4));
        var m = parseInt(datee.substring(5,7));
        var d = parseInt(datee.substring(8,10));
		var freq=parseInt(e.detail.value);
		var dates=[0,31,28,31,30,31,30,31,31,30,31,30,31];
        for(var i=0;i<freq;i++)
        {
            if(d==dates[m])
            {
				if(m!=12)
				{
                	m++;
                	d=1;
				}
				else
				{
					m=1;
					d=1;
					y++;
				}
            }
            else
            {
                d++;
            }
        }
        y=y+"";
        m=m+"";
        d=d+"";
        this.setData({
            nextdate:y+'-'+m+'-'+d
        })
	},
	checknum(x,name)
	{
		for(var i=0;i<x.length;i++)
		{
			if(x[i]<'0'||x[i]>'9')
			{
				wx.showToast({
				  title: name+'格式错误',
				  icon: 'error'
				})
				return false;
			}
		}
		return true;
	},
	bindDateChange(e) {
		this.setData({
		  date: e.detail.value,
		});
		this.checkdate();
	  },
	check()
	{
		var d=this.data.age;
		if(!this.checknum(d,"年龄"))
			return;
		d=this.data.phoneNum;
		if(!this.checknum(d,"手机号"))
			return;
		if(d.length!=11)
		{
			wx.showToast({
			  title: '手机号应为11位',
			  icon:"error"
			})
			return;
		}
        if(this.data.isfreint==0)
        {
            wx.showToast({
              title: '频率应为整数',
              icon:'error'
            })
            return;
        }
        // if(this.data.fretoolong==1)
        // {
        //     wx.showToast({
		// 	  title: '频率过长(<7)',
		// 	  icon:'error'
        //     })
        //     return;
        // }
        var debuggg=0;
        var damissing=new Array(10);
        var flag=0;
        for(var i=0;i<10;i++)
        {
            damissing[i]="";
        }
        if(this.data.name=='')
        {
            damissing[0]="missing";
            flag=1;
            debuggg++;
        }
        if(this.data.sex=='')
        {
            damissing[1]="missing";
            flag=1;
        }
        if(this.data.age==0)
        {
            damissing[3]="missing";
            flag=1;
        }
        if(this.data.phoneNum=='')
        {
            damissing[4]="missing";
            flag=1;
        }
        if(this.data.keshi=="请选择")
        {
            damissing[5]="missing";
            flag=1;
        }
        if(this.data.date=="")
        {
            damissing[6]="missing";
            flag=1;
        }
        // if(this.data.mzh=="")
        // {
        //     damissing[7]="missing";
        //     flag=1;
        // }
        if(this.data.fre=="")
        {
            damissing[8]="missing";
            flag=1;
        }
        if(this.data.zd=="")
        {
            damissing[9]="missing";
            flag=1;
		}
		// if(this.data.bh=="")
        // {
        //     damissing[10]="missing";
        //     flag=1;
        // }
        this.setData({
            datamissing : damissing,
            debugg:debuggg
		})
		if(flag==1)
			return false
		else
			return true
    },
    precover()
    {
        let that=this;
        wx.request({
          url: host+'/doctor/recover',
          method:'PUT',
			header:{
                "token":app.globalData.userInfo_doc.msg,
                'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            },
            data:{
                patientPhone:that.data.phoneNum
            },
            success(res)
            {
                console.log()
                console.log(res);
                wx.showToast({
                  title: '恢复成功',
                  icon:'success'
                })
                wx.navigateBack();
            },
        })
    },
    infoAdd: function()
    {
		if(!this.check())
			return;
        console.log(this.data.damissing)
            console.log("发起请求")
            let that=this
            wx.request({
                url:host+'/doctor/patient',
                method:'POST',
                header:{
                    "token":getApp().globalData.userInfo_doc.msg
                },
                data:
                {
					"patientYbh":this.data.ybh,
					"patientZyh":this.data.zyh,
					"patientBh": this.data.bh,
                    "patientAge": this.data.age,
                    "patientCtime": this.data.date,
                    "patientGender": this.data.sex,
                    "patientId": 0,
                    "patientMzh": this.data.mzh,
                    "patientName": this.data.name,
                    "patientNext": this.data.nextdate,
                    "patientNum": 0,
                    "patientOffice": this.data.keshi,
                    "patientOpenid": '',
                    "patientPhone": this.data.phoneNum,
                    "patientPl": this.data.fre,
                    "patientState": 0,
                    "patientZd": this.data.zd,
					"patientHspt": app.globalData.userInfo_doc.data.doctorHspt,
					"patientProject": this.data.xm
                },
                success:res=>
                {
					console.log(res)
					if(res.data.code==10000)
					{
						wx.switchTab({
						url: '../home',
						});
						wx.showToast({
						title: '添加成功'
						});
					}
					else if(res.data.code==10001)
					{
						if(res.data.msg=="该手机号已被医生录入")
							wx.showToast({
								title: "手机号已录入",
								icon:'error'
							})
						else
						{
							wx.showToast({
							  title: res.data.msg,
							  icon:'error'
							})
						}
                    }
                    else if(res.data.code==10002)
                    {
                        wx.showModal({
                            title:res.data.msg,
                            editable:false,
                            success(res)
                            {
                                console.log(res)
                                if(res.confirm)
                                    that.precover();
                            }
                        })
                    }
					else if(res.statusCode==500)
					{
						wx.showToast({
							title: '服务器错误',
							icon:'error'
							});
					}
					else
					{
						wx.showToast({
						  title: res.data.message,
						  icon:'error'
						})
					}
                },
                fail:res=>
                {
                    console.log(res)
                    wx.showToast({
                      title: '录入失败',
                      icon:"error"
                    })
                }
            })
            // wx.switchTab({
            //   url: '../home/home',
            // });
            // wx.showToast({
            //   title: '添加成功'
            // });
	},
	aniStart:function()
	{
		this.setData({
			idx:0
		})
		let that=this;
		id=setInterval(function(){
			that.aniNext()
		},50)
	},
	aniNext:function()
	{
		var idxx=this.data.idx;
		idxx++
		this.setData({
			idx:idxx
		})
		if(idxx>15)
			clearInterval(id)
	},
	getHsptList()
	{
		let that=this;
		wx.request({
			url: host+'/admin/hosptl',
			method:"GET",
			success(res)
			{
				console.log(res)
				var l=res.data.data;
				var h=app.globalData.userInfo_doc.data.doctorHspt
				console.log(app.globalData.userInfo_doc)
				var idx=0
				for(var i=0;i<l.length;i++)
				{
					if(l[i].hosptlName==h)
					{
						idx=l[i].hosptlId
					}
				}
				that.getOfficeList(idx)
				that.getxmList(idx)
			}
		})
	},
	getxmList(idx)
	{
		let that=this;
		wx.request({
			url: host+'/admin/project',
			method:"GET",
			data:{
				"id":idx
			},
			success(res)
			{
				console.log(res)
				var tmp=[]
				for(var i=0;i<res.data.data.length;i++)
				{
					tmp[i]=res.data.data[i].projectName
				}
				that.setData({
					xms:tmp
				})
			}
		})
	},
	getOfficeList(idx)
	{
		console.log(idx)
		let that=this;
		wx.request({
			url: host+'/admin/office',
			method:"GET",
			data:{
				"id":idx
			},
			success(res)
			{
				console.log(res)
				var olist=res.data.data;
				var l=[]
				for(var i=0;i<olist.length;i++)
				{
					l[i]=olist[i].officeName;
				}
				that.setData({
					offices:l
				})
			}
		})
	},
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
		console.log(this.data.date)
		this.getHsptList()
		// this.setData({
		// 	xms:app.globalData.xms
		// })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
			keshi:app.globalData.userInfo_doc.data.doctorOffice
		})
		this.aniStart()
		var datee=this.data.date;
        var y = parseInt(datee.substring(0,4));
        var m = parseInt(datee.substring(5,7));
        var d = parseInt(datee.substring(8,10));
        var freq=this.data.fre;
        var dates=[0,31,28,31,30,31,30,31,31,30,31,30,31];
        for(var i=0;i<freq;i++)
        {
            if(d==dates[m])
            {
				if(m!=12)
				{
                	m++;
                	d=1;
				}
				else
				{
					m=1;
					d=1;
					y++;
				}
            }
            else
            {
                d++;
            }
        }
        y=y+"";
        m=m+"";
        d=d+"";
        this.setData({
            nextdate:y+'-'+m+'-'+d
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})